package com.dk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.entity.TAdminScoreAccount;
import com.dk.mapper.TAdminScoreAccountMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-11-03
 */
@RestController
@RequestMapping("/account")
public class TAdminScoreAccountController {
//    后台管理用户的积分管理
    @Autowired
    private TAdminScoreAccountMapper accountMapper;

//  积分增加
    @ApiOperation(value = "积分增加")
    @ApiImplicitParams({
            @ApiImplicitParam(name="adminId",value = "用户Id",required = true,paramType = "query"),
            @ApiImplicitParam(name="totalScore",value = "用户总共的积分",required = true,paramType = "query"),
            @ApiImplicitParam(name="expendScore",value = "用户消耗的积分",required = true,paramType = "query")
    })
    @PostMapping("add")
    public Result add(TAdminScoreAccount account){
//        获取填入的用户id
        String adminId = account.getAdminId();
//        获取填入的积分
        Integer totalScore = account.getTotalScore();
//        查询表中所有的数据对比有没有重复的用户id如果有叠加积分进行修改
        List<TAdminScoreAccount> tAdminScoreAccounts = accountMapper.selectList(null);
        for (TAdminScoreAccount adminScoreAccount:tAdminScoreAccounts) {
             String adminId1= adminScoreAccount.getAdminId();
             if (adminId1.equals(adminId)){
                 Integer totalScore1 = adminScoreAccount.getTotalScore();
                 int i2=totalScore+totalScore1;
                 Date date = new Date();
                 int i3=accountMapper.updateByJf(i2,adminId,date);
                 if (i3>0){
                     return Result.bulid(ConstantEnum.SUCCESS).setResult("改用户叠加积分成功");
                 }
                 return Result.bulid(ConstantEnum.ERROR).setResult("改用户叠加积分成功失败");
             }
        }
        account.setCreateTime(new Date());
        account.setUpdateTime(new Date());
        int i = accountMapper.insert(account);
        if (i>0){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("积分增加成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("积分增加失败");
    }

    @ApiOperation(value = "积分修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "积分表的主键id",required = true,paramType = "query"),
            @ApiImplicitParam(name="totalScore",value = "用户总共的积分",required = true,paramType = "query"),
            @ApiImplicitParam(name="expendScore",value = "用户消耗的积分",required = true,paramType = "query"),
    })
    @PostMapping("update")
    public Result update(TAdminScoreAccount account){
        account.setCreateTime(new Date());
        int i = accountMapper.updateById(account);
        if (i>0){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("积分表修改成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("积分表修改失败");
    }

    @ApiOperation(value = "积分删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "积分表的主键id",required = true,paramType = "query")
    })
    @PostMapping("deleteById")
    public Result deleteById(Long id){
        int i = accountMapper.deleteById(id);
        if (i>0){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("删除成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("删除失败");
    }


//  因为积分表数据可能很多但是adminId是不能重复的可以通过adminId进行查询
    @ApiOperation(value = "积分表根据adminId单条查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="adminId",value = "用户Id",required = true,paramType = "query")
    })
    @PostMapping("selectByAdminId")
    public Result selectByAdminId(String adminId){
        TAdminScoreAccount scoreAccount= accountMapper.selectByAdminId(adminId);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(scoreAccount);
    }

//    查询全部
    @ApiOperation(value = "积分表查询全部")
    @PostMapping("findAll")
    public Result findAll(){
        List<TAdminScoreAccount> tAdminScoreAccounts = accountMapper.selectList(null);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(tAdminScoreAccounts);
    }
}
